package com.tanhua.api.impl.mongo;


import com.tanhua.api.mongo.VoiceApi;
import com.tanhua.domain.mongo.Voice;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service
public class VoiceApiImpl implements VoiceApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public void saveVoice(Voice voice) {
        mongoTemplate.insert(voice);
    }

    @Override
    public List<Voice> findVoice(Integer start) {

        Query query = new Query(Criteria.where("start").is(0));
        query.with(new Sort(Sort.Direction.ASC,"created"));
        List<Voice> voices = mongoTemplate.find(query, Voice.class);
        return voices;
    }

    @Override
    public void update(Integer id,String url) {
        Query query1 = new Query(Criteria.where("userId").is(id).and("voiceUrl").is(url));
        Update update = new Update();
        update.set("start",1);
        mongoTemplate.upsert(query1,update, Voice.class);
    }
}
